package com.e2oto.seller.modules.launch.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.e2oto.seller.common.base.BaseActivity;
import com.e2oto.seller.R;
import com.e2oto.seller.common.utils.LogTools;
import com.e2oto.seller.common.utils.PermissionUtils;
import com.e2oto.seller.common.utils.SPtools;
import com.e2oto.seller.common.utils.ToastUtils;

public class GuideActivity extends BaseActivity {

    @Override
    protected int getContentView() {
        return R.layout.activity_guide;
    }

    @Override
    protected void afterView(Bundle savedInstanceState) {
        PermissionUtils.requestPermissions(this, new PermissionUtils.RequestPermissionCallback() {
                    @Override
                    public void allow() {
                        startLocation();
                        startActivity(new Intent(GuideActivity.this, LoginActivity.class));
                    }
                }, Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_PHONE_STATE);


    }

    private void startLocation() {
        //初始化定位
        final AMapLocationClient mLocationClient = new AMapLocationClient(getApplicationContext());
        AMapLocationClientOption mOption = new AMapLocationClientOption();
        mOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//可选，设置定位模式，可选的模式有高精度、仅设备、仅网络。默认为高精度模式
        mOption.setGpsFirst(false);//可选，设置是否gps优先，只在高精度模式下有效。默认关闭
        mOption.setHttpTimeOut(30000);//可选，设置网络请求超时时间。默认为30秒。在仅设备模式下无效
        mOption.setInterval(2000);//可选，设置定位间隔。默认为2秒
        mOption.setNeedAddress(true);//可选，设置是否返回逆地理地址信息。默认是true
        mOption.setOnceLocation(false);//可选，设置是否单次定位。默认是false
        mOption.setOnceLocationLatest(false);//可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        AMapLocationClientOption.setLocationProtocol(AMapLocationClientOption.AMapLocationProtocol.HTTP);//可选， 设置网络请求的协议。可选HTTP或者HTTPS。默认为HTTP
        mOption.setSensorEnable(false);//可选，设置是否使用传感器。默认是false
        mOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
        mLocationClient.setLocationOption(mOption);
        //设置定位回调监听
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (null != aMapLocation) {
                    String province = aMapLocation.getProvince();
                    String city = aMapLocation.getCity();
                    String district = aMapLocation.getDistrict();
                    if (!TextUtils.isEmpty(province) && !TextUtils.isEmpty(city) && !TextUtils.isEmpty(district)) {
                        mLocationClient.stopLocation();
                        SPtools.put(GuideActivity.this, "province", province);
                        SPtools.put(GuideActivity.this, "city", city);
                        SPtools.put(GuideActivity.this, "district", district);
                        LogTools.e("获取到您的地理位置:" + province + city + district);
                    }

                } else {
                    ToastUtils.INSTANCE.showToast(GuideActivity.this, "获取您的地理位置失败");
                }

            }
        });
        mLocationClient.startLocation();
    }


}
